/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.favorites;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.DNDTree.*;
import java.awt.Point;
import java.awt.dnd.DnDConstants;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.favorite.SlimFavorite;

/**
 *
 * @author bfindeisen
 */
public class FavoritesTreeTransferHandler extends AbstractTreeTransferHandler {

    public FavoritesTreeTransferHandler(DNDTreeIFace tree, int action) {
        super(tree, action, true);
    }

    public boolean canPerformAction(DNDTreeIFace target, DefaultMutableTreeNode draggedNode, int action, Point location) {
        TreePath pathTarget = target.getTree().getPathForLocation(location.x, location.y);
        System.out.println("path target:" + pathTarget);

        if (pathTarget == null) {
            target.getTree().setSelectionPath(null);
            return (false);
        }
        target.getTree().setSelectionPath(pathTarget);
        if (action == DnDConstants.ACTION_COPY) {
            return (false);
        } else if (action == DnDConstants.ACTION_MOVE) {
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) pathTarget.getLastPathComponent();


            if (draggedNode.isRoot() ||
                    parentNode == draggedNode.getParent() ||
                    draggedNode.isNodeDescendant(parentNode)) {
                return (false);
            } else {

                if (parentNode.getUserObject() instanceof SlimFavorite) {
                    SlimFavorite fav = (SlimFavorite) parentNode.getUserObject();
                    if (fav.isAudio()) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }

            }
        } else {
            return (false);
        }
    }

    public boolean executeDrop(DNDTreeIFace target, DefaultMutableTreeNode draggedNode, DefaultMutableTreeNode newParentNode, int action) {
        if (action == DnDConstants.ACTION_COPY) {
            DefaultMutableTreeNode newNode = target.makeDeepCopy(draggedNode);
            ((DefaultTreeModel) target.getTree().getModel()).insertNodeInto(newNode, newParentNode, newParentNode.getChildCount());
            TreePath treePath = new TreePath(newNode.getPath());
            target.getTree().scrollPathToVisible(treePath);
            target.getTree().setSelectionPath(treePath);
            return (true);
        }
        if (action == DnDConstants.ACTION_MOVE) {
            draggedNode.removeFromParent();

            SlimFavorite draggedFav = (SlimFavorite) draggedNode.getUserObject();
            SlimFavorite targetFav = (SlimFavorite) newParentNode.getUserObject();
            try {
                JukeboxProperties.getInstance().getFavoritesPlugin().moveFavorite(draggedFav, targetFav);
                ((DefaultTreeModel) target.getTree().getModel()).insertNodeInto(draggedNode, newParentNode, newParentNode.getChildCount());
                TreePath treePath = new TreePath(draggedNode.getPath());
                target.getTree().scrollPathToVisible(treePath);
                target.getTree().setSelectionPath(treePath);
                return (true);
            } catch (SlimException ex) {
                Logger.getLogger(FavoritesTreeTransferHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return (false);
    }
}
